<!-- 采购情况 -->
<template>
	<div class="t-wp" v-loading="state.loading">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form class="text-right" :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="exportExcel()"> 导出 </el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="full-table" shadow="hover">
			<el-table :data="state.list" style="width: 100%" border>
				<el-table-column type="expand">
					<template #default="scope">
						<div style="padding: 10px 30px;">
							<el-table :data="scope.row.partProvidersInfo">
								<el-table-column type="index" label="序号" width="55" align="center" />
								<el-table-column prop="partProviderName" label="参选单位名称" show-overflow-tooltip />
								<el-table-column prop="unifiedSocialCode" label="统一社会信用代码" show-overflow-tooltip />
								<el-table-column prop="offer" label="参选报价" show-overflow-tooltip />
								<el-table-column prop="legalPersonName" label="法定代表人姓名" show-overflow-tooltip />
								<el-table-column prop="legalPersonId" label="法定代表人身份证号" show-overflow-tooltip />
								<el-table-column prop="bailor" label="授权委托人姓名" show-overflow-tooltip />
								<el-table-column prop="bailorIdentityId" label="授权委托人身份证号" show-overflow-tooltip />
								<el-table-column prop="isWin" label="是否中选" show-overflow-tooltip>
									<template #default="scope">
										{{scope.row.isWin ? '是' : '否'}}
									</template>
								</el-table-column>
							</el-table>
						</div>
					</template>
				</el-table-column>
				<el-table-column type="index" label="序号" width="55" align="center" />
				<!-- <el-table-column prop="name" label="采购单位名称" show-overflow-tooltip /> -->
				<el-table-column prop="projectName" label="项目名称" show-overflow-tooltip />
				<el-table-column prop="procurement" label="采购方式" show-overflow-tooltip />
				<el-table-column prop="budget" label="采购限价(元)" show-overflow-tooltip />
				<el-table-column prop="publicDate" label="采购时间" show-overflow-tooltip />
			</el-table>
		</el-card>
	</div>
</template>

<script setup name="SupplierManage">
import { onMounted, reactive, ref } from 'vue';
import {useRouter, useRoute} from "vue-router";
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus';
import {$get, $post, $postparams} from '/@/utils/request';
import * as XLSX from "xlsx-js-style";

const router = useRouter();
const route = useRoute();
const addModalRef = ref();

const state = reactive({
	loading: false,

	list: [],
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	var res = await $get('/projectService/getprojectstatus');
	state.list = res.result;
	state.loading = false;
};

// 导出excel
const exportExcel = () => {
	var head = ['项目名称', '采购方式', '采购限价(元)', '采购时间', '参选单位名称', '统一社会信用代码', '参选报价', '法定代表人姓名', '法定代表人身份证号', '授权委托人姓名', '授权委托人身份证号', '是否中选'];
	var data = [];
	var merges = [];
	for(let i = 0; i < state.list.length; i++) {
		var aItem = state.list[i];
		let childLength = aItem.partProvidersInfo.length;
		if(!childLength) {
			data.push([aItem.projectName,aItem.procurement,aItem.budget,aItem.publicDate,null,null,null,null,null,null,null,null])
		} else {
			// 计算合并单元格
			if(childLength > 1) {
				merges.push({
					s: {c: 0, r: data.length+1},
					e: {c: 0, r: data.length+childLength}
				})
				merges.push({
					s: {c: 1, r: data.length+1},
					e: {c: 1, r: data.length+childLength}
				})
				merges.push({
					s: {c: 2, r: data.length+1},
					e: {c: 2, r: data.length+childLength}
				})
				merges.push({
					s: {c: 3, r: data.length+1},
					e: {c: 3, r: data.length+childLength}
				})
			}
			for(let j = 0; j < childLength; j++) {
				let bItem = aItem.partProvidersInfo[j];
				if(j == 0) {
					data.push([aItem.projectName,aItem.procurement,aItem.budget,aItem.publicDate,bItem.partProviderName,bItem.unifiedSocialCode,bItem.offer,bItem.legalPersonName,bItem.legalPersonId,bItem.bailor,bItem.bailorIdentityId,bItem.isWin]);
				} else {
					data.push([null,null,null,null,bItem.partProviderName,bItem.unifiedSocialCode,bItem.offer,bItem.legalPersonName,bItem.legalPersonId,bItem.bailor,bItem.bailorIdentityId,bItem.isWin]);
				}
			}
		}
	}
	data.unshift(head);

	var ws = XLSX.utils.aoa_to_sheet(data);

	// 设置合并单元格
	ws['!merges'] = merges;

	// 设置单列宽度
	ws['!cols'] = [];
	for(let i = 0; i < head.length; i++) {
		ws['!cols'].push({wpx:150})
	}
	// 设置单行高度
	ws['!rows'] = [];
	for(let i = 0; i < data.length; i++) {
		ws['!rows'].push({hpx:26})
	}

	// 创建一个样式对象
	const style = {
	  alignment: {
	    horizontal: "center", // 设置水平对齐方式为居中
	    vertical: 'center', 
	    wrapText: true
	  }
	};
	// 遍历工作表的单元格，并应用样式
	Object.keys(ws).forEach(cell => {
		if(cell.indexOf('!') != 0) {
			const cellRef = XLSX.utils.decode_cell(cell);
		  const cellStyle = ws[cell].s || {}; // 获取单元格的样式对象
		  cellStyle.alignment = style.alignment; // 应用样式的对齐方式
		  let num = cell.slice(1);
		  if(num <= 1) {
		  	cellStyle.font = { bold: true }
		  	// cellStyle.fill = { fgColor: { rgb: "FFFFAA00" } }
		  }
		  ws[cell].s = cellStyle; // 更新单元格的样式对象
		}
	});
	
	var wb = XLSX.utils.book_new();
	XLSX.utils.book_append_sheet(wb, ws, "sheet1");
	XLSX.writeFile(wb, "采购情况表.xlsx");
}


</script>

<style lang="scss" scoped>
.t-wp {
	
}
</style>
